package com.ksyun.ks3.services.handler;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.ksyun.ks3.exception.Ks3ClientException;
import com.ksyun.ks3.exception.Ks3ServiceException;
import com.ksyun.ks3.model.Owner;
import com.ksyun.ks3.model.Part;
import com.ksyun.ks3.model.result.ListPartsResult;
import com.ksyun.ks3.services.request.Ks3HttpRequest;
import com.ksyun.ks3.services.request.ListPartsRequest;
import com.ksyun.ks3.util.DateUtil;
import com.ksyun.ks3.util.ResponseUtils;
import com.ksyun.ks3.util.StringUtils;

import okhttp3.Headers;

public abstract class ListPartsResponseHandler extends Ks3HttpResponseHandler {

    private static String RESPONSE_TAG_PRIFIX = "ns2:";

    public abstract void onFailure(ListPartsRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException);

    public abstract void onSuccess(ListPartsRequest request, ListPartsResult result);

    @Override
    public final void onSuccess(Ks3HttpRequest request, int statusCode, Headers responseHeaders, byte[] response) {
        ListPartsResult result = parseXml(response);
        result.setStatusCode(statusCode);
        result.setHeaders(ResponseUtils.parseResponseHeaders(responseHeaders));
        onSuccess((ListPartsRequest) request, result);
    }

    @Override
    public final void onFailure(Ks3HttpRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException) {
        this.onFailure((ListPartsRequest) request, clientException, serviceException);
    }

    @Override
    public final void onProgress(long bytesWritten, long totalSize) {
    }

    @Override
    public final void onStart() {
    }

    @Override
    public final void onFinish() {
    }

    @Override
    public final void onCancel() {
    }

    private ListPartsResult parseXml(byte[] response) {
        XmlPullParserFactory factory = null;
        ListPartsResult partsResult = null;
        Owner owner = null;
        Owner initiator = null;
        Part part = null;
        boolean isOwner = false;
        try {
            factory = XmlPullParserFactory.newInstance();
            XmlPullParser parse = factory.newPullParser();
            parse.setInput(new ByteArrayInputStream(response), "UTF-8");
            int eventType = parse.getEventType();
            while (XmlPullParser.END_DOCUMENT != eventType) {
                String nodeName = parse.getName();
                if (!StringUtils.isBlank(nodeName) && nodeName.startsWith(RESPONSE_TAG_PRIFIX)) {
                    nodeName = nodeName.substring(4);
                }
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                    case XmlPullParser.END_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        if ("ListPartsResult".equalsIgnoreCase(nodeName)) {
                            partsResult = new ListPartsResult();
                        }
                        if ("Bucket".equalsIgnoreCase(nodeName)) {
                            partsResult.setBucketname(parse.nextText());
                        }
                        if ("Key".equalsIgnoreCase(nodeName)) {
                            partsResult.setKey(parse.nextText());
                        }
                        if ("UploadId".equalsIgnoreCase(nodeName)) {
                            String upid = parse.nextText();
                            partsResult.setUploadId(upid);
                        }
                        if ("Initiator".equalsIgnoreCase(nodeName)) {
                            initiator = new Owner();
                        }
                        if ("Owner".equalsIgnoreCase(nodeName)) {
                            isOwner = true;
                            owner = new Owner();
                        }
                        if ("ID".equalsIgnoreCase(nodeName)) {
                            if (isOwner) {
                                owner.setId(parse.nextText());
                            } else {
                                initiator.setId(parse.nextText());
                            }
                        }
                        if ("DisplayName".equalsIgnoreCase(nodeName)) {
                            if (isOwner) {
                                owner.setDisplayName(parse.nextText());
                            } else {
                                initiator.setDisplayName(parse.nextText());
                            }
                        }
                        if ("StorageClass".equalsIgnoreCase(nodeName)) {
                        }
                        if ("PartNumberMarker".equalsIgnoreCase(nodeName)) {
                            partsResult.setPartNumberMarker(parse.nextText());
                        }
                        if ("NextPartNumberMarker".equalsIgnoreCase(nodeName)) {
                            partsResult.setNextPartNumberMarker(parse.nextText());
                        }
                        if ("MaxParts".equalsIgnoreCase(nodeName)) {
                            partsResult.setMaxParts(parse.nextText());
                        }
                        if ("IsTruncated".equalsIgnoreCase(nodeName)) {
                            partsResult.setTruncated(Boolean.valueOf(parse.nextText()));
                        }
                        if ("Part".equalsIgnoreCase(nodeName)) {
                            part = new Part();
                        }
                        if ("PartNumber".equalsIgnoreCase(nodeName)) {
                            part.setPartNumber(Integer.valueOf(parse.nextText()));
                        }
                        if ("LastModified".equalsIgnoreCase(nodeName)) {
                            String dateStr = parse.nextText();
                            if (!StringUtils.isBlank(dateStr))
                                part.setLastModified(DateUtil.ConverToDate(dateStr));
                        }
                        if ("ETag".equalsIgnoreCase(nodeName)) {
                            part.setETag(parse.nextText());
                        }
                        if ("Size".equalsIgnoreCase(nodeName)) {
                            part.setSize(Long.valueOf(parse.nextText()));
                        }
                        if ("Encoding-Type".equalsIgnoreCase(nodeName)) {
                            partsResult.setEncodingType(parse.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("ListPartsResult".equalsIgnoreCase(nodeName)) {

                        }
                        if ("Initiator".equalsIgnoreCase(nodeName)) {
                            partsResult.setInitiator(initiator);
                        }
                        if ("Owner".equalsIgnoreCase(nodeName)) {
                            isOwner = false;
                            partsResult.setOwner(owner);
                        }
                        if ("Part".equalsIgnoreCase(nodeName)) {
                            partsResult.getParts().add(part);
                        }
                        break;
                    case XmlPullParser.TEXT:
                        break;
                    default:
                        break;
                }
                eventType = parse.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return partsResult;
    }


}
